<script lang="ts">
export default {
  name: 'Cart'
}
</script>

<script setup lang="ts">
import { message } from '@/components/XtxUI'
import { useCartStore, useMemberStore } from '@/store'
import { storeToRefs } from 'pinia'
import { useRouter } from 'vue-router'

const cartStore = useCartStore()
const {
  isAllSelected,
  effectiveCartCount,
  effectiveCartList,
  selectedCartCount,
  selectedCartPrice
} = storeToRefs(cartStore)
const { getCartList, deleteCart, updateCart, updateCartAllSelected } = cartStore
getCartList()

const memberStore = useMemberStore()
const router = useRouter()
const goCheckoutPage = () => {
  // 判断是否登录
  if (!memberStore.isLogin) {
    return message({ type: 'warn', text: '请先登录' })
  }
  // 判断购物车是否有选中的商品
  if (!cartStore.selectedCartCount) {
    return message({ type: 'warn', text: '请勾选至少一个商品下单' })
  }
  router.push('/member/checkout')
}
</script>

<template>
  <div class="xtx-cart-page">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>购物车</XtxBreadItem>
      </XtxBread>
      <div class="cart">
        <table>
          <!-- 表头 -->
          <thead>
            <tr>
              <th width="120">
                <XtxCheckBox
                  :model-value="isAllSelected"
                  @update:model-value="(val) => updateCartAllSelected({ selected: val })"
                  >全选</XtxCheckBox
                >
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <template v-if="effectiveCartCount > 0">
              <tr v-for="item in effectiveCartList" :key="item.skuId">
                <td>
                  <XtxCheckBox
                    :model-value="item.selected"
                    @update:model-value="(val) => updateCart(item.skuId, { selected: val })"
                  />
                </td>
                <td>
                  <div class="goods">
                    <RouterLink :to="`/goods/${item.id}`">
                      <img :src="item.picture" />
                    </RouterLink>
                    <div>
                      <p class="name ellipsis">{{ item.name }}</p>
                      <p class="attr">{{ item.attrsText }}</p>
                    </div>
                  </div>
                </td>
                <td class="tc">
                  <p>&yen;{{ item.nowPrice }}</p>
                </td>
                <td class="tc">
                  <XtxCount
                    :model-value="item.count"
                    @update:model-value="(val) => updateCart(item.skuId, { count: val })"
                    :max="item.stock"
                  />
                </td>
                <td class="tc">
                  <p class="f16 red">&yen;{{ (Number(item.nowPrice) * item.count).toFixed(2) }}</p>
                </td>
                <td class="tc">
                  <p><a href="javascript:;">移入收藏夹</a></p>
                  <p>
                    <a class="green" href="javascript:;" @click="deleteCart({ ids: [item.skuId] })"
                      >删除</a
                    >
                  </p>
                  <p><a href="javascript:;">找相似</a></p>
                </td>
              </tr>
            </template>
            <template v-else>
              <tr>
                <td colspan="6">
                  <div class="cart-none">
                    <img src="@/assets/images/none.png" />
                    <p>购物车内暂时没有商品</p>
                    <div class="btn">
                      <XtxButton type="primary" @click="$router.push('/')"> 继续逛逛 </XtxButton>
                    </div>
                  </div>
                </td>
              </tr>
            </template>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch"></div>
        <div class="total">
          共 {{ effectiveCartCount }} 件有效商品，已选择 {{ selectedCartCount }} 件，商品合计：
          <span class="red">¥{{ selectedCartPrice }}</span>
          <XtxButton type="primary" @click="goCheckoutPage">下单结算</XtxButton>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.tc {
  text-align: center;

  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}

.red {
  color: @priceColor;
}

.green {
  color: @xtxColor;
}

.f16 {
  font-size: 16px;
}

.tit {
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
  color: #666;
}

.xtx-cart-page {
  .cart {
    color: #666;
    background: #fff;

    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;

      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;

        &:first-child {
          padding-left: 30px;
          color: #999;
          text-align: left;
        }
      }

      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}

.goods {
  display: flex;
  align-items: center;

  img {
    width: 100px;
    height: 100px;
  }

  > div {
    padding-left: 10px;
    width: 280px;
    font-size: 16px;

    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}

.cart-none {
  padding: 50px 0px;
  text-align: center;

  p {
    color: #999;
  }

  .btn {
    margin-top: 20px;
  }
}

.action {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding: 0 30px;
  height: 80px;
  font-size: 16px;
  background: #fff;

  .xtx-checkbox {
    color: #999;
  }

  .batch {
    a {
      margin-left: 20px;
    }
  }

  .red {
    margin-right: 20px;
    font-size: 18px;
    font-weight: bold;
  }
}
</style>
